   ***data paths 
  global archive /Volumes/Sans titre/DATA_MICROEU
  global posted /Volumes/Sans titre/DATA_MICROEU/Posting
  global data /Users/Mathilde/Dropbox/Posted_workers/Data
  global replication_outputs_a /Users/Mathilde/Dropbox/Posted_workers/Replication/Appendix_figures
  global replication_estimates /Users/Mathilde/Dropbox/Posted_workers/Replication/Main_estimates
  global replication_estimates_a /Users/Mathilde/Dropbox/Posted_workers/Replication/Appendix_estimates

   
   
   **output paths
  global replication_outputs_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_figures
  global replication_estimates /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_estimates
  global replication_estimates_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_estimates
  global replication_outputs /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_figures
  
  
  *********************************************************************************************
  * This dataset uses EU-LFS data on EU migrants to reproduce supplementary figures A2, A3, B10, B11 and B6 
  **********************************************************************************************
    
  
  
  
  *****************************************************************************
  * Figure B9: Use Raw LFS data to produce stock of foreign citizen by receiving country
  ******************************************************************************
  
  
  *stock of foreign citizen by year and receiving country; years 2004-2016
  *No information "NMS" origin before 2004 in the EU-LFS 
  
   foreach var in lu fr de be nl it ie pt uk es gr dk at fi se pl sk cz ee si lt lv ro bg{
   forval x=2004/2016{
   use "$archive/LFS 3/`var'`x'.dta", clear
   keep if age>17
   keep if age<66
   destring ilostat, replace
   keep if ilostat==1
   *******
   g mig_EU15=0
   replace mig_EU15=1*coeff*1000 if national=="001-EU15"
   g mig_NMS1=0
   replace mig_NMS1=1*coeff*1000 if national=="002-NMS10"
   g mig_NMS2=0
   replace mig_NMS2=1*coeff*1000 if national=="002-NMS10"
   replace mig_NMS2=1*coeff*1000 if national=="003-NMS3"
   replace mig_NMS2=1*coeff*1000 if national=="004-NMS13"
   g mig_all=0
   replace mig_all=1*coeff*1000 if national!="000-OWN COUNTRY"
   *******
   replace coeff=coeff*1000
   collapse (sum)  mig_NMS* mig_EU15 mig_all coeff, by(year country)  
   save "$posted/`var'`x'_stockimmi.dta", replace
   }
   }
     
	 
     foreach var in lu fr de be nl it ie pt uk es gr dk at fi se pl sk cz ee si lt lv ro bg{
	 use "$posted/`var'2004_stockimmi.dta", clear
	 forval x=2005/2016{
	 append using "$posted/`var'`x'_stockimmi.dta"
	 }
	 save "$posted/`var'_stockimmi.dta", replace
	 }
	 
	 use "$posted/lu_stockimmi.dta", clear
     foreach var in fr de be nl it ie pt uk es gr dk at fi se pl sk cz ee si lt lv ro bg{
	 	append using "$posted/`var'_stockimmi.dta"
	 }
	 rename country receiving
	 destring year, replace
	 save "$posted/panel_immigEULFS.dta", replace
	 
	 g s=mig_NMS1/coeff*100
	 destring year, replace
	 *Appendix Figure D28
	 twoway (connected s year if receiving=="FR" & year<2016), graphregion(fcolor(white) lcolor(white)) ylabel(0 (.3) .6) ///
	 xline(2008, lpattern(dash)) xlabel(2004 (1) 2015, angle(vertical)) xtitle("") ytitle("Stock of NMS migrants in French active population (%)")
     graph export "$replication_outputs_a/FigureD29.pdf", replace 
	 
	
	*****************************************************************
	* Create bilateral matrice of migration flows within EU countries
	*****************************************************************
	 
   *list of EU countries after liberalization events 
   global nms PL SK CZ EE SI LT LV HU RO BG HR AT BE BG CH DE DK ES FI FR GR IE IT LI LU NL NO PT SE UK
   
   *group of destination countries 
   foreach var in lu fr de be nl it ie pt uk es gr dk at fi se{
   forval x=2000/2016{
   use "$archive/LFS 3/`var'`x'.dta", clear
   *working-age migration inflows
   keep if age>17
   keep if age<66
   destring ilostat, replace
   *active pop
   keep if ilostat==1
   *keep only EU origin countries
   g tag=0
   foreach y in $nms{
   replace tag=1 if COUNTR1Y=="`y'"
   }
   replace tag=1 if COUNTR1Y==country
   replace tag=1 if missing(COUNTR1Y)
   keep if tag==1
   *weighting variable from EU-LFS
   replace coeff=coeff*1000
   rename COUNTR1Y sending
   collapse (sum) coeff, by(year country sending)  
   save "$posted/`var'`x'_flows.dta", replace
   }
   }
   
   
   foreach var in lu fr de be nl it ie pt uk es gr dk at fi se{
   use "$posted/`var'2000_flows.dta", clear
   forval x=2001/2016{
   append using  "$posted/`var'`x'_flows.dta"
    }
   rename country receiving
   save "$posted/`var'_flows.dta", replace
   }
   

   
  *Imputation following Caliendo et al. (2021) Journal of political economy
  *migration matrix is sparse -- imputation for missing flows at the origin-destination-year level
  
  
  foreach x in lu fr de be nl it ie pt uk es gr dk at fi se{
  use "$posted/`x'_flows.dta", clear
  egen ID=group(sending receiving)
  destring year, replace
  tsset ID year  
  drop if missing(sending)
  tsfill
  encode sending, g(sendingnum)
  replace sending=sending[_n-1] if missing(sending)
  replace receiving=receiving[_n-1] if missing(receiving)
  *interpolate 
  by ID: ipolate coeff year, gen(flow_i)  
  drop sendingnum
  save "$posted/`x'_flows_i.dta", replace
  }
  
  
  *Restrict to the same 4 destination countries than in Figure 1 to get comparable results 
  use "$posted/fr_flows_i.dta", clear
  append using "$posted/de_flows_i.dta"
  append using "$posted/at_flows_i.dta"
  append using "$posted/be_flows_i.dta"
  save "$posted/panel_migflows_fratdebe.dta", replace
  rename sending country
  rename receiving destination
  *merge with dataset from Figure 1 on postings 
  merge 1:1 year destination country using "$data_raw/figure1_regression.dta"
  rename country sending 
  *drop "stayers" because we do not observe for postings. 
  drop if sending==destination
  reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(pair)
  g logmig=log(flow_i)
  
  reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year) cl(pair)
  est store posting
  reghdfe logmig coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year) cl(pair)
  est store mig
  
   coefplot (posting,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) label("Posting")) ///
   (mig,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) label("Migration (free movement)")), ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Flows) ///
   legend(ring(0) pos(11) cols(1)) ylabel(-4 (2) 4)
   
  
   
  reghdfe logposted coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(pair)
  est store posting2
  reghdfe logmig coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up, absorb(pair year destinationnum##year countrynum##year) cl(pair)
  est store mig2
  
   coefplot (posting2,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) label("Posting")) ///
   (mig2,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) label("Migration (free movement)")), ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Flows) ///
   legend(ring(0) pos(11) cols(1)) ylabel(-4 (2) 4)
	
	
   coefplot (mig,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) label("Pair FE, Origin-Year FE")) ///
   (mig2,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22 ) label("Pair FE, Origin-Year FE, Destination-Year FE")), ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Flows) ///
   legend(ring(0) pos(11) cols(1)) ylabel(-4 (2) 4)
	
 
   g indic_mig=0
   replace indic_mig=1 if destination=="DE"
   replace indic_mig=1 if destination=="AT"
   
   **************
   *FIGURE B9 
   **************
   
   reghdfe logmig coeff_bottom coeff_10-coeff_12 zero coeff_14-coeff_22 coeff_up if indic_mig==0, absorb(pair year destinationnum##year) cl(pair)
coefplot,  keep( coeff_10 coeff_11 coeff_12 ///
   zero coeff_14 coeff_25 coeff_16 coeff_17 coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) ///
   omit vertical ///
   graphregion(fcolor(white) lcolor(white))   ciopts(recast(rcap) lwidth(medthick))  ///
   xline(3.5, lcolor(red)) yline(0, lcolor(gs10) lpattern(dash)) xtitle("Years to Posting Liberalization") ytitle(Log Migration Flows) ///
   legend(ring(0) pos(11) cols(1)) ylabel(-4 (2) 4) legend(off)
   *appendix figure 
   graph export "$replication_outputs_a/FigureB9.pdf", replace
   
  ***************************************************
  * Appendix Figures B10-B11, A2-A3 
  **************************************************

   
   

  global varlist2 PL HU EE LT LV SK CZ FR DE ES BE PT UK LU AT RO BG NL IT IE HR DK SE FI 
  local nms1 SI SK LT LV HU CZ PL EE 
  local nms2 RO BG 
  local nms3 HR nms3 
  local event1 2003
  local event2 2006
  local event1bis 2010
  local event3 2013
  local event2bis 2013
  local eu15  BE FR CH LU PT ES IT UK FI SE EL IE NO NL 
  local eu15bis  AT DE
  
  

   *dataset merging iso codes 3 and 2 digits 
   import delimited "$data/country-list_zip/data/data_csv.csv", varnames(1) clear 
   replace code="UK" if code=="GB"
   save "$data/name_iso2.dta", replace

  * Raw datasets from ILO 
  
  import excel "$data/ILR_CBCT_NOC_RT_A_EN.xlsx", sheet("Sheet1") firstrow clear
  drop E
  rename Referencearea name 
  merge m:1 name using "$data/name_iso2.dta"
  keep if _m==3
  drop _m
  drop Source
  rename Time year 
  rename Value collective_rate 
  rename code country
  save "$data/collective_rate.dta", replace
  
  import excel "$data/ILR_TUMT_NOC_RT_A_EN.xlsx", sheet("Sheet1") firstrow clear
  drop E
  rename Referencearea name 
  merge m:1 name using "$data/name_iso2.dta"
  keep if _m==3
  drop _m
  drop Source
  rename Time year 
  rename Value union
  rename code country
  save "$data/union_rate.dta", replace
  
  
  import excel "$data/STR_DAYS_ECO_RT_A_EN.xlsx", sheet("Sheet1") firstrow clear
  drop E
  rename Referencearea name 
  merge m:1 name using "$data/name_iso2.dta"
  keep if _m==3
  drop _m
  drop Source
  rename Time year 
  rename Total strike
  rename code country
  save "$data/strike_rate.dta", replace
  
  *Raw data from Eurostat 
  
  import delimited "$data/lfsa_urgacob__custom_3822926_linear.csv", varnames(1) clear 
drop dataflow lastupdate obs_flag age sex unit
encode c_birth, g(nat)
  drop c_birth
  rename obs_value unemp
  drop freq
  reshape wide unemp, i(geo time_period) j(nat)
  rename time_period year
  rename geo country
  g diffunemp=unemp1-unemp2
  save "$data/unemp_rate.dta", replace
  
   import excel "$data/Migrant_integration_statistics_-_over-qualification_MI2021_v3.xlsx", sheet("Sheet1") firstrow clear 
   drop F G H
   drop if missing(country_name)
   g diffEU=EU2010-NAT2010
   g diffFOR=FOR2010-NAT2010
   save "$data/qualif_rate.dta", replace
   
   
   
   *Raw data on a12 posting forms at the bilateral level collected from the EC 
   
   use "$data/SI_bilateal.dta", clear 
   foreach y in $varlist2{
   append using "$data/`y'_bilateal.dta"
   }
   
   drop if receiving=="ATDE"
   drop if receiving=="all_butDEAT"
   drop if receiving=="Unknown"
   drop if receiving=="total"
   drop if receiving=="total1"
   drop if receiving=="total2"
   drop if receiving=="total3"
   drop if receiving=="total4"
   drop if receiving=="no_border"
   drop if receiving=="all"
   drop if receiving=="total5"
   rename n posted
   drop if sending==receiving
   drop P n_graph Q R S
   egen tot_sent=sum(posted), by(sending year)
   g s_sending=posted/tot_sent
   save "$data/matrix_a12.dta", replace
   
   *Raw data on duration of posting assignements 
   *for a13 posting (more than one destination country)
   import excel "$data/scaleofposting_EU.xlsx", sheet("dur_a13")  firstrow clear
   drop O P 
   reshape long n, i(country) j(year)
   rename n dur_a13
   rename country sending
   save "$data/dur_a13.dta", replace
   *for a12 posting (one destination country)
   import excel "$data/scaleofposting_EU.xlsx", sheet("dur_a12")  firstrow clear
   drop O P 
   reshape long n, i(country) j(year)
   rename n dur_a12
   rename country sending
   save "$data/dur_a12.dta", replace
   
   *total number of sent a13 postings by sending EU country from the EC dataset  
   import excel "$data/scaleofposting_EU.xlsx", sheet("a13") cellrange(A1:P34) firstrow clear
   reshape long n, i(country) j(year)
   rename n a13
   drop if country=="Total"
   rename country sending
   save "$data/a13.dta", replace
   
    *Raw data on active population in the EU   
   import excel "$data/scaleofposting_EU.xlsx", sheet("pop_active")  firstrow clear
   rename TIME country
   reshape long n, i(country) j(year)
   rename n pop
   drop if country=="total"
   save "$data/pop.dta", replace
   
   
   use "$data/matrix_a12.dta", clear 
   merge m:1 sending year using  "$data/a13.dta"
   drop if _m==2
   drop _m
   *allocate a13 posting forms to sending countries based on the patterns observed for a12 postings
   g posted_a13=s_sending*a13
   replace posted_a13=0 if missing(posted_a13)
   g posted_all=posted+posted_a13
   
   *adjusting for duration (full time equivalents)
   merge m:1 sending year using "$data/dur_a12.dta"
   keep if _m==3
   drop _m
   merge m:1 sending year using "$data/dur_a13.dta"
   keep if _m==3
   drop _m  
   g posted_all_dur=(posted*dur_a12/365)+(posted_a13*dur_a13/365)
   save "$data/matrix_all.dta", replace
   
   
   
   
  global nms1 SI SK LT LV HU CZ PL EE 
  global nms2 RO BG 
  global nms3 HR 
  
  use "$data/matrix_all.dta", clear 
  g NMS1=0
  foreach y in $nms1{
  replace NMS1=1 if sending=="`y'"
  }
  g NMS2=0
  foreach y in $nms2{
  replace NMS2=1 if sending=="`y'"
  }
  g NMS3=0
  foreach y in $nm3{
  replace NMS3=1 if sending=="`y'"
  } 
  g NMS1_r=0
  foreach y in $nms1{
  replace NMS1_r=1 if receiving=="`y'"
  }
  g NMS2_r=0
  foreach y in $nms2{
  replace NMS2_r=1 if receiving=="`y'"
  }
  g NMS3_r=0
  foreach y in $nms3{
  replace NMS3_r=1 if receiving=="`y'"
  }
  
  *****
  g post_ref=0
  replace post_ref=2004 if NMS1==1
  replace post_ref=2011 if NMS1==1 & receiving=="AT"
  replace post_ref=2011 if NMS1==1 & receiving=="DE"
  
  replace post_ref=2007 if NMS2==1
  replace post_ref=2014 if NMS2==1 & receiving=="AT"
  replace post_ref=2011 if NMS2==1 & receiving=="DE"
  
  g mig_ref=0
  replace mig_ref=2004 if receiving=="UK"  & NMS1==1
  replace mig_ref=2004   if receiving=="IE"  & NMS1==1
  replace mig_ref=2004  if receiving=="SE"  & NMS1==1
  
  replace mig_ref=2006 if receiving=="ES"  & NMS1==1
  replace mig_ref=2006  if receiving=="EL"  & NMS1==1
  replace mig_ref=2006  if receiving=="IT"  & NMS1==1
  replace mig_ref=2006 if receiving=="PT"  & NMS1==1
  replace mig_ref=2006  if receiving=="FI"  & NMS1==1
  
  replace mig_ref=2007  if receiving=="LU" & NMS1==1
  replace mig_ref=2007  if receiving=="NL" & NMS1==1
  
  replace mig_ref=2008  if receiving=="FR" & NMS1==1
  replace mig_ref=2009  if receiving=="BE"  & NMS1==1
  replace mig_ref=2009  if receiving=="DK"  & NMS1==1
  replace mig_ref=2011  if receiving=="AT"  & NMS1==1
  replace mig_ref=2011  if receiving=="DE"  & NMS1==1
  
  egen total_received=sum(posted_all), by(receiving year)
  egen total_received_dur=sum(posted_all_dur), by(receiving year)
 
  
  preserve
  
  keep if NMS1==1
  drop if NMS1_r==1
  drop if NMS2_r==1
  drop if NMS3_r==1
  g dist=mig_ref-post_ref
  drop if mig_ref==0
  collapse (sum) posted_all (mean) dist total_received mig_ref, by(year receiving)
  g s=posted_all/total_received
  rename receiving country
  merge 1:1 year country using "$data/pop.dta"
  drop if _m==2
  g sp=posted_all/(pop*1000)*100
  rename country receiving
  scatter sp dist if year==2017 , mlabel(receiving)
  restore
  
  *sum all posting inflows from NMS by destination and year for 2006-2017 
  g NMS=NMS1+NMS2+NMS3
  egen posted_all_durnms=sum(posted_all_dur) if NMS==1, by(receiving year)
  collapse (sum) posted_all posted_all_dur (mean) posted_all_durnms (max) mig_ref, by(receiving year)
  rename receiving country
  *Eurostat data on working age population
  merge 1:1 year country using "$data/pop.dta"
  drop if _m==2
  drop _m
  rename country receiving 
  save "$data/receiving_year_allpostings.dta", replace
  
  
 ****merging migration data****
 
 use "$data/receiving_year_allpostings.dta", clear 
 *this was created in line 61 of this code 
 merge 1:1 receiving year using "$posted/panel_immigEULFS.dta"
 *keep if _m==3
 *Remember, mig_NMS2 is stock of working-age foreigners from 13 NMS countries that joined the EU since 2004
 *mig_NMS1 is restricted to the 8 Eastern European countries that joined in 2004 (does not include RO,BG,HR)
 
 g s_post=posted_all/(mig_EU15+mig_NMS2)*100
 g s_post_fte=(posted_all_dur/(mig_EU15+mig_NMS2))*100
 g s_post_fte_nms=(posted_all_durnms/(mig_NMS2))*100

 g s_post_inpop=(posted_all_dur/coeff)*100
 g s_mig_inpop=(mig_EU15+mig_NMS2)/coeff*100
 
 g nms=0
 foreach y in $nms1 $nms2 $nms3{
 	replace nms=1 if receiving=="`y'"
 }
 
 graph bar s_post_inpop if year==2015 & _m==3 & nms==0, over(receiving, sort(s_post_inpop)) graphregion(fcolor(white) lcolor(white)) ///
 ytitle(Posted workers (FTE) in Active population (%))

 *Figure A2 
 graph bar s_post_fte if year==2015 & nms==0 & receiving!="PT" & _m==3, over(receiving, sort(s_post_fte))   graphregion(fcolor(white) lcolor(white)) ///
 ytitle(Posted workers (FTE)/ Stock of Active Migrants (%))
 graph export "$replication_outputs_a/FigureA2.pdf", replace 

  ******
  
  collapse (sum) posted_all posted_all_dur posted_all_durnms mig_EU15 mig_NMS2 mig_NMS1 mig_all coeff (mean) nms (max) mig_ref, by(year receiving)
  
  
  g s_nms=posted_all_durnms/mig_NMS2*100
  g s_EU15=(posted_all_dur-posted_all_durnms)/(mig_EU15)
  g s_nms2=posted_all_durnms/mig_NMS1
  
  *Figure A3
  *Panel A
  graph bar s_nms if year==2015 & nms==0 & s_nms!=., over(receiving, sort(s_nms))  graphregion(fcolor(white) lcolor(white)) ///
 ytitle("Posted workers from NMS (FTE)" "in Stock of Active Migrants from NMS (%)")
 graph export "$replication_outputs_a/FigureA3a.pdf", replace 

  *Look at change in NMS migrant stocks since enlargement -> additional workforce flow from NMS since 2004
  *No data for 2004: use 2005 instead 
  *For posting its already a flow, so just look at posting inflows in the post-enlargement period. 
  
  g initmig_d=mig_NMS2 if year==2005
  egen initmig=min(initmig), by(receiving)
  g s_mignmsdelta=posted_all_durnms/(mig_NMS2-initmig)*100
  g delta=(mig_NMS2-initmig)
  
  *Figure A3, Panel B 
  graph bar s_mignmsdelta if year==2015 & nms==0 & s_nms!=., over(receiving, sort(s_mignmsdelta))  graphregion(fcolor(white) lcolor(white)) ///
 ytitle("Inflows of Posted workers from NMS (FTE)" "in total number of NMS migrants arrived since 2004 (%)")
 graph export "$replication_outputs_a/FigureA3b.pdf", replace 
 
 
 
 twoway (scatter s_mignmsdelta mig_ref if year==2015 & nms==0 & mig_ref!=0, mlabel(receiving)) (lfit s_mignmsdelta mig_ref if year==2015 & nms==0 & mig_ref!=0, lcolor(red)), xlabel(2004 (1) 2011) ///
 graphregion(fcolor(white) lcolor(white)) xtitle("Year of Free movement liberalization for NMS of 2004") ///
 ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)

 
 
 **************Correlate with gravity and labor market indicator****************************
 
 
 reg s_mignmsdelta mig_ref if year==2015, ro
 rename receiving country
 *Eurostat public data on employers labor cost 
 merge 1:1 year country using "$data/labourcostpanelwithmin.dta"
 g ssc=cost/wage*100
 
 twoway (scatter s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0, mlabel(country)) (lfit s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0), 

  twoway (scatter s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0, mlabel(country)) (lfit s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0), graphregion(fcolor(white) lcolor(white)) xtitle("Employers' payroll tax rate") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
  
  
  *Figure B11, Panel A
   twoway (scatter s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT", mlabel(country)) (lfit s_mignmsdelta ssc if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT", lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Employers' payroll tax rate") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "$replication_outputs_a/FigureB11a.pdf", replace 
  

 drop _m
 merge 1:1 year country using "$data/collective_rate.dta"
 drop if _m==2
 egen meancollec=mean(collective_rate) if year>2013 & year<2017, by(country)
 
 
  twoway (scatter s_mignmsdelta meancollec if year==2015 & nms==0 & mig_ref!=0, mlabel(country)) (lfit s_mignmsdelta meancollec if year==2015 & nms==0 & mig_ref!=0, lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Collective bargaining coverage rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
  
    *Figure B11, Panel B 
   twoway (scatter s_mignmsdelta meancollec if year==2015 & nms==0 & mig_ref!=0   & country!="ES" & country!="IT" & country!="PT" & country!="GR", mlabel(country)) (lfit s_mignmsdelta meancollec if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Collective bargaining coverage rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "$replication_outputs_a/FigureB11b.pdf", replace 
  

  
  drop _m
  merge 1:1 year country using "$data/union_rate.dta"
  drop if _m==2

 twoway (scatter s_mignmsdelta union if year==2015 & nms==0 & mig_ref!=0, mlabel(country)) (lfit s_mignmsdelta union if year==2015 & nms==0 & mig_ref!=0), graphregion(fcolor(white) lcolor(white)) xtitle("Union rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "/Users/Mathilde/Dropbox/Posted_workers/Paper/Figures/binsc_postedinmig_nmsdelta_union.pdf", replace 
 
     *Figure B11, Panel C
  twoway (scatter s_mignmsdelta union if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", mlabel(country)) (lfit s_mignmsdelta union if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Union rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "$replication_outputs_a/FigureB11c.pdf", replace 
 

  drop _m
  merge 1:1 year country using "$data/unemp_rate.dta"
  drop if _m==2
  
   twoway (scatter s_mignmsdelta diffunemp if year==2015 & nms==0 & mig_ref!=0, mlabel(country)) (lfit s_mignmsdelta diffunemp if year==2015 & nms==0 & mig_ref!=0), graphregion(fcolor(white) lcolor(white)) xtitle("Migrants unemployment rate-Natives unemployment rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
  
  *Figure B10, Panel A
   twoway (scatter s_mignmsdelta diffunemp if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", mlabel(country)) (lfit s_mignmsdelta diffunemp if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Migrants unemployment rate-Natives unemployment rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "$replication_outputs_a/FigureB10a.pdf", replace 
  
  
  

  drop _m
  merge m:1 country using "$data/qualif_rate.dta"
  drop if _m==2
  
    *Figure B10, Panel B
  twoway (scatter s_mignmsdelta diffFOR if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT" & country!="GR", mlabel(country)) (lfit s_mignmsdelta diffFOR if year==2015 & nms==0 & mig_ref!=0 & country!="ES" & country!="IT" & country!="PT"  & country!="GR", lcolor(red)), graphregion(fcolor(white) lcolor(white)) xtitle("Migrants overqualification rate-Natives overqualification rate (%)") ytitle("Inflows of Posted workers from NMS (FTE)" "in total stock of NMS migrants arrived since 2004 (%)") legend(off)
 graph export "$replication_outputs_a/FigureB10b.pdf", replace 

  
 


   
   ************************************************
   * additional appendix material: figure b6
   *************************************************
   
   
global NMS1 PL SK SI HU EE CZ LV LT 
global NMS2 RO BG
global NMS3 HR 




*****************************************

 
 import excel "$data/EUaccession_ES.xlsx", sheet("all2") firstrow clear
 tab country 
 drop if country=="total"
 
 g treated=0
 replace treated=1 if EVENT>0
 
 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 drop NMS1 NMS2 NMS3
 
 keep if destination=="FR"
 foreach y in $NMS1{
 	replace country="NMS" if country=="`y'"
 }
 collapse (sum) n, by(country year)
 
 *Figure B6 panel A
 twoway (connected n year if country=="NMS",lcolor(cranberry) mcolor(cranberry)) ///
 (connected n year if country=="NL",lcolor(dknavy) msymbol(T) mcolor(dknavy)) ///
 (connected n year if country=="UK",mcolor(dknavy) lcolor(dknavy) lpattern(dash)) ///
 (connected n year if country=="IT", lpattern(dash) mcolor(gs10) lcolor(gs10)), graphregion(fcolor(white) lcolor(white)) legend(label(1 "NMS 2004") label(2 "Netherlands")  label(3 "United Kingdom") label(4 "Italy") cols(1) ring(0) pos(11)) 
    graph export "$replication_outputs_a/FigureB6a.pdf", replace 



 import excel "$data/EUaccession_ES.xlsx", sheet("all2") firstrow clear
 tab country 
 drop if country=="total"
 g treated=0
 replace treated=1 if EVENT>0
 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 keep if destination=="AT"
 foreach y in $NMS1{
 	replace NMS1=1 if country=="`y'"
 }
 
  foreach y in $NMS2{
 	replace NMS2=1 if country=="`y'"
 }
 
   foreach y in $NMS3{
 	replace NMS3=1 if country=="`y'"
 }
 
 g EU15=0
 replace EU15=1 if NMS1==0 & NMS2==0 & NMS3==0
 
 drop if NMS2==1 
 drop if NMS3==1 
 
 collapse (sum) n, by(NMS1 year)
 
  gen norm_graph=n if year==2010
  bys NMS1: egen norm_graph_d=min(norm_graph)
  g n_graph_norm=n/norm_graph_d
  sort year
  *Figure B6, panel B
  twoway (connected n_graph_norm year if NMS1==1 & year>2005, mcolor(cranberry) lcolor(cranberry)) ///
  (connected n_graph_norm year if NMS1==0 & year>2005, mcolor(dknavy) lcolor(dknavy)), ///
  graphregion(fcolor(white) lcolor(white)) ylabel(0 (2) 8) ///
  xlabel(2006 (1) 2017, angle(vertical)) xtitle("") ytitle("Workers Posted to Austria (2010=1)") xline(2010.5,    lcolor(red)) ///
  legend(label(1 "Treated= NMS 2004") label(2 "Control= EU-15") cols(1) pos(11) ring(0))
   graph export "$replication_outputs_a/FigureB6b.pdf", replace 

  
 import excel "$data/EUaccession_ES.xlsx", sheet("all2") firstrow clear
 tab country 
 drop if country=="total"
 g treated=0
 replace treated=1 if EVENT>0
 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 keep if destination=="FR"
 foreach y in $NMS1{
 	replace NMS1=1 if country=="`y'"
 }
 
  foreach y in $NMS2{
 	replace NMS2=1 if country=="`y'"
 }
 
   foreach y in $NMS3{
 	replace NMS3=1 if country=="`y'"
 }
 
 g EU15=0
 replace EU15=1 if NMS1==0 & NMS2==0 & NMS3==0
  drop if NMS1==1 
  drop if NMS3==1 
  collapse (sum) n, by(NMS2 year)
  gen norm_graph=n if year==2006
  bys NMS2: egen norm_graph_d=min(norm_graph)
  g n_graph_norm=n/norm_graph_d
  sort year
  *Figure B6, panel C
  twoway (connected n_graph_norm year if NMS2==1 & year>2002 , mcolor(cranberry) lcolor(cranberry)) ///
  (connected n_graph_norm year if NMS2==0 & year>2002 , mcolor(dknavy) lcolor(dknavy)), ///
  graphregion(fcolor(white) lcolor(white)) ylabel(0 (20) 80) ///
  xlabel(2003 (1) 2016, angle(vertical)) xtitle("") ytitle("Workers Posted to France (2006=1)") xline(2006.5,    lcolor(red)) ///
  legend(label(1 "From NMS 2007") label(2 "From EU-15") cols(1) pos(11) ring(0))
     graph export "$replication_outputs_a/FigureB6c.pdf", replace 

  
 import excel "$data/EUaccession_ES.xlsx", sheet("all2") firstrow clear
 tab country 
 drop if country=="total"
 g treated=0
 replace treated=1 if EVENT>0
 destring n*, replace force
 reshape long n, i(country destination NMS1 NMS2 NMS3 treated EVENT) j(year)
 keep if destination=="AT"
 foreach y in $NMS1{
 	replace NMS1=1 if country=="`y'"
 }
 
  foreach y in $NMS2{
 	replace NMS2=1 if country=="`y'"
 }
 
   foreach y in $NMS3{
 	replace NMS3=1 if country=="`y'"
 }
 
 g EU15=0
 replace EU15=1 if NMS1==0 & NMS2==0 & NMS3==0
  drop if NMS1==1 
  drop if NMS3==1 
  collapse (sum) n, by(NMS2 year)
  gen norm_graph=n if year==2013
  bys NMS2: egen norm_graph_d=min(norm_graph)
  g n_graph_norm=n/norm_graph_d
  sort year
  *Figure B6, panel D
  twoway (connected n_graph_norm year if NMS2==1 & year>2005 , mcolor(cranberry) lcolor(cranberry)) ///
  (connected n_graph_norm year if NMS2==0 & year>2005 , mcolor(dknavy) lcolor(dknavy)), ///
  graphregion(fcolor(white) lcolor(white)) ylabel(0 (10) 20) ///
  xlabel(2006 (1) 2016, angle(vertical)) xtitle("") ytitle("Workers Posted to Austria (2010=1)") xline(2006.5,    lcolor(red)) ///
  legend(label(1 "From NMS 2007") label(2 "From EU-15") cols(1) pos(11) ring(0))
  graph export "$replication_outputs_a/FigureB6d.pdf", replace 

   
  